package com.whatever;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class TextBoxPracticeActivity extends Activity implements OnClickListener {
    
	public int[] textBoxMap = { //Array to define the Id of each textBox
			R.id.editText0,
			R.id.editText1,
			R.id.editText2,
			R.id.editText3,
	};
	
	public int[] labelMap = { //Array to define the Id of each label
			R.id.textView0,
			R.id.textView1,
			R.id.textView2,
			R.id.textView3,
			R.id.textView4,
	};
	
	public int[] buttonMap = { //Array to define the Id of each button
			R.id.button0
	};
	
	public int[] spinnerMap = {
			R.id.spinner0
	};
	
	//Declare all variables and objects
	public EditText[] textBox;
	public TextView[] label;
	public Spinner spinner;
	public Button button;
	public ArrayAdapter adapter;
	public String output;
	public int btnId;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Define variables and objects
        textBox = new EditText[4];
        label = new TextView[5];
        button = (Button)findViewById(R.id.button0);
        spinner = (Spinner)findViewById(spinnerMap[0]);
        adapter = ArrayAdapter.createFromResource(this, R.array.courses, android.R.layout.simple_spinner_item);
        output = "";
        
        //Define array objects using for loops
        for(int i = 0; i < textBox.length; i++) { //For every index in the textbox array...
        	textBox[i] = (EditText)findViewById(textBoxMap[i]); //Initialize the textbox in array index "i"
        }
        
        for(int i = 0; i < label.length; i++) { //For every index in the label array...
        	label[i] = (TextView)findViewById(labelMap[i]); //Initialize the label in array index "i"
        }
        
        spinner.setAdapter(adapter);
        button.setOnClickListener(this);
        setLabels();
        
    }
    
    public void setLabels () {
    	label[0].setText("Name: ");
    	label[1].setText("Age: ");
    	label[2].setText("Hometown: ");
    	label[3].setText("Room #: ");
    	label[4].setText("Course: ");
    }

	public void onClick(View v) {
		btnId = v.getId();
		output = "";
		
		for(int i = 0; i < buttonMap.length; i++) {
			if(btnId == buttonMap[i]) {
				btnId = i;
			}
		}
		
		if(btnId == 0) {
			for(int i = 0; i < textBox.length; i++) {
				output = output + label[i].getText().toString() + textBox[i].getText().toString() + "\n";
				if(i == textBox.length -1) {
					output = output + spinner.getSelectedItem().toString();
				}
			}
			Toast.makeText(getApplicationContext(), output, Toast.LENGTH_SHORT).show();
		}
	}
}